package com.tangqi.data;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;

/**
 *
 *
 *
 * @author applerockhool
 * @version 2010-4-29
 */
public class MarketPriceUtil {

    public void getStockPrice(){

      String url = "http://hq.sinajs.cn/list=sh600000,sz000002,sz150018";
        try {
            URL u = new URL(url);
            byte[] b = new byte[256];
            InputStream in = null;
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
//            while (true) {
                try {
                    in = u.openStream();
                    int i;
                    while ((i = in.read(b)) != -1) {
                        bo.write(b, 0, i);
                    }
                    String result = bo.toString();
                    String[] stocks = result.split(";");
                    for (String stock : stocks) {
                        int seq=0;
                        String[] datas = stock.split(",");
                        //根据对照自己对应数据
                        for (String data : datas) {
                            System.out.println((++seq)+":\t"+data);
                        }
                        System.out.println("--------------------");
                    }
                    bo.reset();
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                } finally {
                    if (in != null) {
                        in.close();
                    }
                }
//            }
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }


    public static double getSingleStockPrice(String stockCode){
        double price = 0;

        String url = "http://hq.sinajs.cn/list="+stockCode;
        try {
            URL u = new URL(url);
            byte[] b = new byte[256];
            InputStream in = null;
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
                try {
                    in = u.openStream();
                    int i;
                    while ((i = in.read(b)) != -1) {
                        bo.write(b, 0, i);
                    }
                    //
                    String stockInfo = bo.toString();
                    System.out.println("bo:"+bo);
//                    String[] stocks = result.split(";");
//                    for (String stock : stocks) {
                        int seq=0;
                        String[] datas = stockInfo.split(",");
                        //根据对照自己对应数据
                        for (String data : datas) {
                            if(seq == 3){
                                return Double.valueOf(data);
                            }
                            seq++;
//                            System.out.println((seq++)+":\t"+data);
                        }
                        System.out.println("--------------------");
//                    }
                    bo.reset();
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                } finally {
                    if (in != null) {
                        in.close();
                    }
                }
//            }
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }

        return price;
    }

    /**
     *
     * @param args
     */
    public static void main(String[] args) {
        MarketPriceUtil mp = new MarketPriceUtil();
        double price = mp.getSingleStockPrice("161014");
        System.out.println("market price:"+price);
    }
	
}

